package com.school.campus.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.school.campus.domain.Club;
import com.school.campus.domain.vo.ClubApplicationVO;
import com.school.campus.mapper.ClubMapper;
import com.school.common.utils.DateUtils;
import com.school.common.utils.SecurityUtils;
import com.school.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.school.campus.mapper.ClubApplicationMapper;
import com.school.campus.domain.ClubApplication;
import com.school.campus.service.IClubApplicationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 社团申请Service业务层处理
 *
 * @author wwj
 * @date 2025-11-06
 */
@Service
public class ClubApplicationServiceImpl implements IClubApplicationService {
    @Autowired
    private ClubApplicationMapper clubApplicationMapper;

    @Autowired
    private ClubMapper clubMapper;

    /**
     * 查询社团申请
     *
     * @param id 社团申请主键
     * @return 社团申请
     */
    @Override
    public ClubApplication selectClubApplicationById(Long id) {
        return clubApplicationMapper.selectClubApplicationById(id);
    }

    /**
     * 查询社团申请列表
     *
     * @param clubApplication 社团申请
     * @return 社团申请
     */
    @Override
    public List<ClubApplication> selectClubApplicationList(ClubApplication clubApplication) {
        return clubApplicationMapper.selectClubApplicationList(clubApplication);
    }

    /**
     * 新增社团申请
     *
     * @param clubApplication 社团申请
     * @return 结果
     */
    @Override
    public int insertClubApplication(ClubApplication clubApplication) {
        clubApplication.setApplicantId(SecurityUtils.getUserId());
        clubApplication.setApplicantName(SecurityUtils.getUsername());

        clubApplication.setApplyTime(DateUtils.getNowDate());
        clubApplication.setStatus(0L);
        return clubApplicationMapper.insertClubApplication(clubApplication);
    }

    /**
     * 修改社团申请
     *
     * @param clubApplication 社团申请
     * @return 结果
     */
    @Override
    public int updateClubApplication(ClubApplication clubApplication) {
        return clubApplicationMapper.updateClubApplication(clubApplication);
    }

    /**
     * 批量删除社团申请
     *
     * @param ids 需要删除的社团申请主键
     * @return 结果
     */
    @Override
    public int deleteClubApplicationByIds(Long[] ids) {
        return clubApplicationMapper.deleteClubApplicationByIds(ids);
    }

    /**
     * 删除社团申请信息
     *
     * @param id 社团申请主键
     * @return 结果
     */
    @Override
    public int deleteClubApplicationById(Long id) {
        return clubApplicationMapper.deleteClubApplicationById(id);
    }

    @Override
    public List<List<ClubApplicationVO>> selectClubApplicationListVOByUserId(Long userId) {
        Club clubByCurrentUser = new Club();


        clubByCurrentUser.setDirectorId(userId);
        List<List<ClubApplicationVO>> clubApplicationList = new ArrayList<>();
        // 查看当前用户所负责的社团
        List<Club> clubList = clubMapper.selectClubList(clubByCurrentUser);
        if (!clubList.isEmpty()) {
            for (Club club : clubList) {
                int i = clubApplicationMapper.countByClubId(club.getId());
                if (i > 0) {
                    // 获取当前社团申请信息

                    ClubApplication clubApplication = new ClubApplication();
                    clubApplication.setClubId(club.getId());
                    List<ClubApplicationVO> clubApplications = clubApplicationMapper.selectClubApplicationVOList(clubApplication);
                    clubApplicationList.add(clubApplications);
                }
            }
        }

        return clubApplicationList;
    }

    @Override
    @Transactional
    public int dealWithApplication(Long id, Long status) {
        ClubApplication clubApplication = clubApplicationMapper.selectClubApplicationById(id);
        clubApplication.setStatus(status);
        int i = clubApplicationMapper.updateClubApplication(clubApplication);
        Club club = clubMapper.selectClubById(clubApplication.getClubId());


        club.setMemberCount(status == 1L ? club.getMemberCount() + 1 : club.getMemberCount());
        clubMapper.updateClub(club);
        return i;
    }
}
